![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
babel-plugin-transform-function-bind
Advanced tools
The babel-plugin-transform-function-bind package is a Babel plugin that allows you to use the bind operator (::) in your JavaScript code. This operator provides a shorthand for binding functions to a specific context, making your code more concise and readable.
Function Binding
This feature allows you to bind a function to a specific context using the :: operator. In the example, the greet function is bound to the obj context, ensuring that this.name refers to 'Alice' when the function is called.
const obj = {
name: 'Alice',
greet() {
console.log(`Hello, ${this.name}!`);
}
};
const greet = obj::obj.greet;
greet(); // Output: Hello, Alice!
Method Extraction
This feature allows you to extract a method from an object and bind it to a different context. In the example, the greet method is extracted from obj and then called with a different context, resulting in 'Hello, Charlie!'.
const obj = {
name: 'Bob',
greet() {
console.log(`Hello, ${this.name}!`);
}
};
const greet = ::obj.greet;
greet.call({ name: 'Charlie' }); // Output: Hello, Charlie!
This package allows you to use class properties in your JavaScript code. While it doesn't provide the same function binding capabilities as babel-plugin-transform-function-bind, it does offer a way to define methods and properties directly within a class, which can sometimes be used to achieve similar results.
Compile the new function bind operator
::
to ES5.
obj::func
// is equivalent to:
func.bind(obj)
obj::func(val)
// is equivalent to:
func.call(obj, val)
::obj.func(val)
// is equivalent to:
func.call(obj, val)
const box = {
weight: 2,
getWeight() { return this.weight; },
};
const { getWeight } = box;
console.log(box.getWeight()); // prints '2'
const bigBox = { weight: 10 };
console.log(bigBox::getWeight()); // prints '10'
// Can be chained:
function add(val) { return this + val; }
console.log(bigBox::getWeight()::add(5)); // prints '15'
document.querySelectorAll
It can be very handy when used with document.querySelectorAll
:
const { map, filter } = Array.prototype;
let sslUrls = document.querySelectorAll('a')
::map(node => node.href)
::filter(href => href.substring(0, 5) === 'https');
console.log(sslUrls);
document.querySelectorAll
returns a NodeList
element which is not a plain array, so you normally can't use the map
function on it, and have to use it this way: Array.prototype.map.call(document.querySelectorAll(...), node => { ... })
. The above code using the ::
will work because it is equivalent to:
const { map, filter } = Array.prototype;
let sslUrls = document.querySelectorAll('a');
sslUrls = map.call(sslUrls, node => node.href);
sslUrls = filter.call(sslUrls, href => href.substring(0, 5) === 'https');
console.log(sslUrls);
When nothing is specified before the ::
operator, the function is bound to its object:
$('.some-link').on('click', ::view.reset);
// is equivalent to:
$('.some-link').on('click', view.reset.bind(view));
npm install --save-dev babel-plugin-transform-function-bind
.babelrc
(Recommended).babelrc
{
"plugins": ["transform-function-bind"]
}
babel --plugins transform-function-bind script.js
require("babel-core").transform("code", {
plugins: ["transform-function-bind"]
});
FAQs
Compile function bind operator to ES5
We found that babel-plugin-transform-function-bind demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.